﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Discovery;
using Devtm.ServiceModel.Proxy;
using System.ServiceModel;

namespace Devtm.ServiceModel.Criterias
{


    /// <summary>
    /// Build the request criteria on Discovery service.
    /// </summary>
    public class ServiceCriteria
    {

        private Type Contract;
        private IServiceDirectories probes;

        public ServiceCriteria(Type contract, IServiceDirectories probes)
        {
            // TODO: Complete member initialization
            this.Contract = contract;
            this.probes = probes;
        }

        public FindResponse FindResponse()
        {

            FindResponse findResponse = null;

            var d = probes.Get<DiscoveryEndpoint>();
            if (d != null)
            {

                DiscoveryClient discoveryClient = new DiscoveryClient(d);

                try
                {
                    var criteria = new FindCriteria(Contract);
                    //criteria.Extensions.Add(new System.Xml.Linq.XElement(""));
                    findResponse = discoveryClient.Find(criteria);
                }
                catch (FaultException)
                {

                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    Console.WriteLine("This client was unable to connect to and query the proxy. Ensure that the proxy is up and running.");
                }

            }

            return findResponse;

        }

    }
}
